const localData = localStorage.getItem('pzadmin')
const state =localData?localData.menu: {
  isCollapse: false,
  selectMenu: [],
  routerList:[],
  menuAcitve:'1-1'
}

const mutations = {
  collapseMenu(state) {
    state.isCollapse = !state.isCollapse
  },
  addMenu(state, payload) {
    state.selectMenu.findIndex((item) => item.path === payload.path) === -1 && state.selectMenu.push(payload)
    // console.log('state.selectMenu', state.selectMenu)
  },
  // 删除菜单
  closeMenu(state,payload){
    // console.log('payload', payload)
    state.selectMenu = state.selectMenu.filter(item => item.name !== payload.name)
  },
  // 存储当前用户的权限信息
  dynamicMenu(state,payload){
    // console.log('payload', payload)
    const modules= import.meta.glob('../views/**/**/index.vue')
    // console.log('modules', modules)
    // 教学
    function routerSet(router){
      router.forEach(router => {
        // 判断是否有子菜单
        if(!router.children){
          const url = `../views${router.meta.path}/index.vue`
          router.component = modules[url]
        }else{
          // 如果有子菜单，递归调用
          routerSet(router.children)
        }
      });
      state.routerList = router
    }
    routerSet(payload)
    // console.log('state.routerList', state.routerList)
  },
  // 设置当前激活的菜单
  updateMenuActive(state,payload){
    state.menuActive = payload
  }
}

export default { state, mutations }
